package org.dromara.bean.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.dromara.bean.domain.HisPaymentLog;
import org.dromara.bean.domain.pay.OrderModel;
import org.dromara.bean.domain.vo.Result;
import org.dromara.bean.mapper.BasePaymentLogMapper;
import org.dromara.bean.service.IBasePaymentLogService;
import org.dromara.bean.service.IHisMemberService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.ReturnOrderModel;
import org.dromara.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * @Description: 支付日志
 * @Author: jeecg-boot
 * @Date: 2020-08-03
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class BasePaymentLogServiceImpl extends ServiceImpl<BasePaymentLogMapper, HisPaymentLog> implements IBasePaymentLogService {


   private final BasePaymentLogMapper basePaymentLogMapper;

    private final IHisMemberService baseMemberService;

    BasePaymentLogMapper paymentLogMapper;

    @Override
    public IPage<HisPaymentLog> pageList(Page<HisPaymentLog> page) {
        return basePaymentLogMapper.selectPageList(page);
    }

    @Override
    public HisPaymentLog getByOrderNumber(String orderNo) {
        return basePaymentLogMapper.getByOrderNumber(orderNo);
    }

    /**
     * 1.修改支付日志记录HisPaymentLog
     *
     * @param resultMag
     */
    @Override
    public HisPaymentLog updatePaymentLog(R<?> resultMag) {
        ReturnOrderModel returnOrderModel = (ReturnOrderModel) resultMag.getData();
        String orderNo = returnOrderModel.getOrderNumber();
        //修改状态
        HisPaymentLog hisPaymentLog = this.getByOrderNumber(orderNo);
        if (hisPaymentLog == null) {
            return null;
        }
        hisPaymentLog.setErrCode(returnOrderModel.getMsgCode());
        hisPaymentLog.setUpdateTime(DateUtils.getNowDate());
        hisPaymentLog.setErrCodeDes(returnOrderModel.getMsgCodeDes());
        hisPaymentLog.setPayTime(DateUtils.getNowDate());
        hisPaymentLog.setSuccess(String.valueOf(returnOrderModel.getResultCode()));
        this.updateById(hisPaymentLog);
        //返回aboutId
        return hisPaymentLog;
    }

    /**
     * 增加日志
     *
     * @param orderModel
     * @return
     */
    @Override
    public int insertPaymentLog(OrderModel orderModel) {
        HisPaymentLog byOrderNumber = getByOrderNumber(orderModel.getOrderNumber());
        if (ObjectUtil.isNotEmpty(byOrderNumber)) {
            removeById(byOrderNumber);
        }
        HisPaymentLog hisPaymentLog = new HisPaymentLog();
        hisPaymentLog.setCreateTime(DateUtils.getNowDate());
        hisPaymentLog.setUpdateTime(DateUtils.getNowDate());
//        TODO 此处应该是独立的编号 20231218 需要单独开发 关联微信回调
        hisPaymentLog.setOrderNumber(orderModel.getOrderNumber());
        hisPaymentLog.setType(orderModel.getPayType());//默认微信支付
        hisPaymentLog.setTotalFee(orderModel.getTotalFee());
        hisPaymentLog.setPayTime(DateUtils.getNowDate());
        hisPaymentLog.setAboutId(orderModel.getOrderId());
        hisPaymentLog.setAboutType(orderModel.getAboutType());

        hisPaymentLog.setPrePayId(orderModel.getPrePayId());
        String remark = "用户id:" + orderModel.getOpenId() + "支付 订单号：[" + orderModel.getOrderNumber() + "]" + orderModel.getTotalFee() + "元";
        hisPaymentLog.setRemark(remark);
        return paymentLogMapper.insert(hisPaymentLog);
    }
}
